﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WPFDemoMVVM.Enums
{
    internal class EnumDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return DependencyProperty.UnsetValue;
            }
            return GetEnumDescription(value);
        }

        

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Empty;
        }


        private object GetEnumDescription(object value)
        {
            var fi = value.GetType().GetField(value.ToString());

            var attributes = fi.GetCustomAttribute<DescriptionAttribute>(false);
            if (attributes != null )
            {
                return attributes.Description;
            }
            else
            {
                return value.ToString();
            }
        }
    }
}
